package com.spark.tags;

import java.util.Map;

import com.spark.core.SparkContext;
import com.spark.core.XMap;

/**
 * 
 * @version $Id: NumberField.java v 1.0 2010-12-27 下午04:24:04 IBM Exp $
 * @author <a href="mailto:njq.niu@hand-china.com">vincent</a>
 */
public class NumberField extends TextField {
	
	private static final String DEFAULT_TEMPLATES = "numberField.ftl";
	
	public static final String PROPERTITY_ALLOWDECIMALS = "allowDecimals";
	public static final String PROPERTITY_DECIMALPRECISION = "decimalPrecision";	
	public static final String PROPERTITY_ALLOWNEGATIVE = "allowNegative";
	public static final String PROPERTITY_ALLOWFORMAT = "allowFormat";
	
	
	public String getTemplate(){
		return DEFAULT_TEMPLATES;
	}
	
	public void init(XMap view, SparkContext context) throws Exception{
		super.init(view, context);
		
		if(!view.getBoolean(PROPERTITY_ALLOWDECIMALS, true)) context.addJsonConfig(PROPERTITY_ALLOWDECIMALS, false);
		if(!view.getBoolean(PROPERTITY_ALLOWNEGATIVE, true)) context.addJsonConfig(PROPERTITY_ALLOWNEGATIVE, false);
		if(view.getBoolean(PROPERTITY_ALLOWFORMAT, false)) context.addJsonConfig(PROPERTITY_ALLOWFORMAT, true);
		context.addJsonConfig(PROPERTITY_DECIMALPRECISION, view.getInt(PROPERTITY_DECIMALPRECISION, 2));
	}
	
	@SuppressWarnings("unchecked")
	protected String build(SparkContext context){
		Map map = context.getMap();
		StringBuilder sb = new StringBuilder();
		sb.append("<div class=\"").append(get(map,"className")).append("\" style=\"width:").append(get(map,"width")).append("px;height:").append(get(map,"height")).append("px;").append(get(map,"style")).append("\" id=\"").append(get(map,"id")).append("\">\r");
		sb.append("    <div><input type=\"").append(get(map,"inputType")).append("\" class=\"s_tf s_nf\" stype=\"input\" style=\"width:").append(get(map,"inputWidth")).append("px;line-height:").append(get(map,"lineHeight")).append("px;height:").append(get(map,"inputHeight")).append("px;").append(get(map,"inputStyle")).append("\" ").append(get(map,"readOnly")).append(" autocomplete=\"off\" value=\"").append(get(map,"value")).append("\"/></div>	\r");
		sb.append("</div>\r");
		sb.append("<script>new Spark.NumberField(").append(get(map,"config")).append(");</script>\r");
		return sb.toString();
	}
	
}
